package com.kaifamiao.filter;

import org.tinylog.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HelloFilter implements Filter {

    private FilterConfig config;

    @Override
    public void init(FilterConfig config) throws ServletException {
        Logger.trace("初始化过滤器: " + config.getFilterName());
        this.config = config;
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        String uri = request.getRequestURI();
        Logger.trace(config.getFilterName() + " 正在执行过滤 " + uri );
        // 在 chain.doFilter 之前可以对 请求 和 响应 进行预处理
        chain.doFilter(req, resp);
        // 在 chain.doFilter 之后可以对 响应 进行后处理
    }

    @Override
    public void destroy() {
        Logger.trace( "释放过滤器("+config.getFilterName()+")占用的资源" );
    }

}
